<template>
  <div>
    <!-- labei -->
    <label v-if="label">{{ label }}</label>
    <!-- 内容 -->
    <slot></slot>
    <!-- {{form.rules[prop]}} -->
    <!-- 校验和错误信息展示 -->
    <p v-show="error">{{ error }}</p>
  </div>
</template>

<script>
import Validator from "async-validator";
export default {
  inject: ["form"],
  props: {
    label: {
      type: String,
      default: "",
    },
    prop: {
      type: String,
    },
  },
  data() {
    return {
      error: "",
    };
  },
  mounted() {
    this.$on("validate", () => {
      this.validate();
    });
  },
  methods: {
    validate() {
      const rules = this.form.rules[this.prop];
      const value = this.form.model[this.prop];
      // async-validator
      const validator = new Validator({[this.prop]: rules});
      // 校验时传入数据源
      return validator.validate({[this.prop]: value}, errors => {
        if(errors) {
          this.error = errors[0].message
        } else {
          this.error = ''
        }
      })
    },
  },
};
</script>

<style>
</style>